<?php require_once('Connections/Connection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Recordset1 = 5;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_Connection, $Connection);
$query_Recordset1 = "SELECT id, header, content FROM articles";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $Connection) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?>





<?php require "functions.php" ?>
<?php f_header("Quotes") ?>

<article class="content">
  <h1>Quotes</h1>
  
  <div class="margin">
    <p class="nav">
      <?php $first = TRUE; ?>
      <?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
      <?php if (!$first) { echo "&nbsp;|&nbsp;&nbsp;"; }
		else { $first = FALSE; } ?>
      <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>">First</a>
      <?php } // Show if not first page ?>
      <?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
      <?php if (!$first) { echo "&nbsp;|&nbsp;&nbsp;"; }
		else { $first = FALSE; } ?>
      <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>
      <?php } // Show if not first page ?>
      <?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
      <?php if (!$first) { echo "&nbsp;|&nbsp;&nbsp;"; }
		else { $first = FALSE; } ?>
      <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a>
      <?php } // Show if not last page ?>
      <?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
      <?php if (!$first) { echo "&nbsp;|&nbsp;&nbsp;"; }
		else { $first = FALSE; } ?>
      <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>">Last</a>
      <?php } // Show if not last page ?>
    </p>
  </div>
  
  
  <?php if ($row_Recordset1['content'] != '') { do {
	  
	  
	$id = $row_Recordset1['id'];
	?>
    
  <div class="art_template">
    <p>
      <?php
  $content = $row_Recordset1['content'];
  if (strlen($content) > 400) { $content = substr($content, 0, 400)." ..."; }
  echo $content;
  ?>
    </p>
    <p class="author"><?php echo $row_Recordset1['header']; ?></p>
  </div>
  
  
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); } ?>
  
  
  <div class="margin">
    <p class="nav">
      <?php $first = TRUE; ?>
      <?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
      <?php if (!$first) { echo "&nbsp;|&nbsp;&nbsp;"; }
		else { $first = FALSE; } ?>
      <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>">First</a>
      <?php } // Show if not first page ?>
      <?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
      <?php if (!$first) { echo "&nbsp;|&nbsp;&nbsp;"; }
		else { $first = FALSE; } ?>
      <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>
      <?php } // Show if not first page ?>
      <?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
      <?php if (!$first) { echo "&nbsp;|&nbsp;&nbsp;"; }
		else { $first = FALSE; } ?>
      <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a>
      <?php } // Show if not last page ?>
      <?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
      <?php if (!$first) { echo "&nbsp;|&nbsp;&nbsp;"; }
		else { $first = FALSE; } ?>
      <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>">Last</a>
      <?php } // Show if not last page ?>
    </p>
  </div>
  
</article><!-- end .content -->

<aside>
  <h1 class="asideh">Add Quote</h1>
  
  <?php 
$comm=@mysql_connect($hostname_Connection,$username_Connection,$password_Connection);
$rs=@mysql_select_db($database_Connection) or die( "Unable to select database");
$updated = !empty($_POST['header']) && !empty($_POST['content']);
if ($updated) {
$header = $_POST['header'];
$content = $_POST['content'];
$sql="INSERT INTO articles(header, content) VALUES ('$header','$content')";

if ($updated) echo '<div class="update">Article has been added.</div>';

$result=mysql_query($sql) or die("Insert Error: ".mysql_error());
mysql_close();

if (!(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)) {
	echo '<script> setTimeout("location.reload(true);",2000); </script>';
}
else {
	echo '<a href="articles.php">Obnoviť stránku</a>';
}

}
?>

  <form action="articles.php" method="post">
    Author:<input name="header" type="text" class="input"><br>
    Quote:<br><textarea name="content" class="input"></textarea><br>
    <input type="submit" value="Add" class="button">
    <input type="reset" value="Earse" class="button">
  </form>
  
</aside>

<?php f_footer() ?>